--
-- Author: shunguo.chen
-- Date: 2016-06-20 12:47:35
--
-- MailView

local ListViewExtend = require("app.widget.ListViewExtend")
local IconRender = require("app.widget.renders.IconRender")

local MailRender = import(".renders.MailRender")
local MailWritePanel = import(".MailWritePanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MailScene

function ClassRef:init()
	-- btns
	self._ui.Button_close:addClickEventListener(handler(self, self.onBtnClick))
	self._ui.Button_del:addClickEventListener(handler(self, self.onBtnClick))
	self._ui.Button_get:addClickEventListener(handler(self, self.onBtnClick))
	self._ui.Button_delAll:addClickEventListener(handler(self, self.onBtnClick))
	self._ui.Button_getAll:addClickEventListener(handler(self, self.onBtnClick))

	-- left mail list
	local function scrollViewEvent(sender, evenType)
		if evenType == ccui.ScrollviewEventType.bounceBottom then
			executeFunc(self._onOperate, "reqMails", 2)
		elseif evenType ==  ccui.ScrollviewEventType.bounceTop then
			executeFunc(self._onOperate, "reqMails", 1)
		end
	end
	self._mailList = self._ui.ListView_mail
	ListViewExtend.scrollToItemExtend(self._mailList)
	self._mailList:removeAllItems()
	self._mailList:addScrollViewEventListener(scrollViewEvent)
	self._mailList.createRender = function (list, idx)
		return MailRender.create()
	end

	-- left content
	self._content = self._ui.Panel_right
	self._content:setVisible(false)
	self._rewardPanel = self._content:getChildByName("Panel_reward")
	self._itemList = self._rewardPanel:getChildByName("ListView_item")
	self._itemList:removeAllItems()

	self._contSV = self._content:getChildByName("ScrollView_text")
	self._contSize = self._contSV:getContentSize()
	local fontSize = self._contSV:getChildByName("Text"):getFontSize()
	local fontColor = self._contSV:getChildByName("Text"):getTextColor()
	self._contSV:removeAllChildren()

	self._defaults = {}
	-- self._defaults["KEY_WRAP_MODE"] = 1
	self._defaults["KEY_VERTICAL_SPACE"] = 3
	self._defaults["KEY_FONT_FACE"] = Res.font
	self._defaults["KEY_FONT_SIZE"] = fontSize
	self._defaults["KEY_FONT_COLOR_STRING"] = display.stringWithColor(fontColor)

	self._ui.Text_name = self._content:getChildByName("Text_name")
	self._ui.Text_from = self._content:getChildByName("Text_from")
	self._ui.Text_time = self._content:getChildByName("Text_time")
	self._ui.Text_tips = self._content:getChildByName("Text_tips")
end

function ClassRef:onBtnClick(sender)
	local name = sender:getName()
	-- print("MailView btn click = " .. name)
	if name == "Button_close" then
		executeFunc(self._onCloseView)
	elseif name == "Button_write" then
		executeFunc(self._onOperate, "openWritePanel")
	elseif name == "Button_getAll" then
		executeFunc(self._onOperate, "getItems")
	elseif name == "Button_delAll" then
		executeFunc(self._onOperate, "delete")
	elseif name == "Button_get" then
		if cc.isAlive(self._curMail) then
			local mailData = self._curMail:getData()
			executeFunc(self._onOperate, "getItems", mailData.uid)
		end
	elseif name == "Button_del" then
		if cc.isAlive(self._curMail) then
			local mailData = self._curMail:getData()
			executeFunc(self._onOperate, "delete", mailData.uid)
		end	
	end
end

function ClassRef:getMailList()
	return self._mailList
end

function ClassRef:removeCurMail()
	self._curMail = nil
end

function ClassRef:setCurMail(mail)
	-- 没有选中的邮件时 默认选择第一个邮件
	if mail == nil and not cc.isAlive(self._curMail) then
		local items = self._mailList:getItems()
		if #items > 0 then
			mail = items[1]
		end
	end

	if cc.isAlive(self._curMail) then
		self._curMail:setSelected(false)
	end
	self._curMail = mail
	if cc.isAlive(self._curMail) then
		self._curMail:setSelected(true)
		self._mailList:scrollToItem(self._curMail)

		if self._curMail and self._curMail:getData() and not self._curMail:getData():isGetDetail() then 
			executeFunc(self._onOperate, "reqDetail", self._curMail:getData().uid)
		else 
			self:refreshMailContent()
		end
	else
		self:refreshMailContent()
	end
end

function ClassRef:refreshMailContent()
	if cc.isAlive(self._curMail) and self._curMail:getData() then
		self._ui.Text_empty:setVisible(false)
		self._content:setVisible(true)
		local mailData = self._curMail:getData()
		-- dump(mailData, "MailRecvPanel refreshMailContent mailData")

		-- show title
		self._ui.Text_name:setString(mailData.subjectStr)
		self._ui.Text_from:setString(mailData.sender)
		self._ui.Text_time:setString(mailData.createdTimeStr)
		self._ui.Text_tips:setString(mailData:getOverTimeStr())

		local canGetItems = mailData:canGetItems()
		self._rewardPanel:setVisible(canGetItems)
		self._ui.Button_get:setVisible(canGetItems)
		self._ui.Button_del:setVisible(not canGetItems)

		-- show items
		if canGetItems then
			local height = self._itemList:getContentSize().height
			local index = 0
			local items = self._itemList:getItems()
			local function addItem(data)
				index = index + 1
				local item = items[index]
				if item == nil then
					item = IconRender.create()
					self._itemList:pushBackCustomItem(item)
				end
				-- item:setNewScale(height)
				item:setData(data)
				item:setTipEnable(true, {x=2/5, y=1/2})
				return item
			end

			for i,v in ipairs(mailData.rewards) do
				addItem(v)
			end

			-- remove no used item
			if index < #items then
				for i=1,#items-index do
					self._itemList:removeLastItem()
				end
			end

			if mailData:hasPresentCoin() then
				executeFunc(self._onOperate, "checkPermission")
			end
		end

		-- show content
		local size = self._contSize
		if self._richText and not tolua.isnull(self._richText) then
			self._richText:removeFromParent()
		end
		self._richText = nil
		
		local text = mailData.bodyStr
		local richText = ccui.RichText:createWithXML(text, self._defaults)
		if nil == richText then
			self._contSV:setInnerContainerSize(size)
			return
		end

		richText:setAnchorPoint(cc.p(0, 1))
		richText:ignoreContentAdaptWithSize(false)
		richText:setOpenUrlHandler(function(sender, url)
			executeFunc(self._onOperate, "openURl", url)
		end)
		self._richText = richText

		richText:setContentSize(cc.size(size.width, 0))
		richText:formatText()
		local t_size = richText:getFormatSize()
		richText:setContentSize(t_size)

		if t_size.height > size.height then
			richText:setPosition(0, t_size.height)
			self._contSV:setInnerContainerSize(t_size)
		else
			richText:setPosition(0, size.height)
			self._contSV:setInnerContainerSize(size)
		end

		self._contSV:addChild(richText)

		-- dump(t_size, "t_size")
		-- dump(size, "bg.size")
		-- dump(cc.p(self._richText:getPosition()), "text.pos")
	else
		self._content:setVisible(false)
		self._ui.Text_empty:setVisible(true)
	end
end

return ClassRef